package org.yoki.edu.bg.radius.web.api.impl.rest.client;

import org.yoki.edu.bg.radius.web.api.client.command.ClientCreateCommand;
import org.yoki.edu.bg.radius.web.api.client.command.ClientPageCommand;
import org.yoki.edu.bg.radius.web.api.command.IdsCommand;
import org.yoki.edu.bg.radius.web.api.impl.rest.BaseController;
import com.yunzhitx.cloud.common.model.InvokeResult;
import com.yunzhitx.cloud.common.model.PageResult;
import org.yoki.edu.bg.radius.application.client.ClientApplication;
import org.yoki.edu.bg.radius.core.client.domain.Client;
import org.yoki.edu.bg.radius.vo.client.ClientVO;
import org.yoki.edu.bg.radius.web.api.impl.rest.client.assembler.ClientAssembler;
import org.yoki.edu.bg.radius.web.api.client.ClientService;
import org.yoki.edu.bg.radius.web.api.client.command.ClientUpdateCommand;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;


/**
 * 代码生成器自动生成
 * Date:2018-1-29 17:48:47
 *
 * @author
 */
@RestController
public class ClientController extends BaseController implements ClientService {

    @Inject
    private ClientApplication application;

    /* ************************************************************************************ *
       ***************************************  增  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult<ClientVO> add(@RequestBody @Valid ClientCreateCommand command) {
        Client entity = ClientAssembler.toClient(command);
        entity.setOperatorId(getLoginId());
        entity.setOperatorName("测试管理员");
        ClientVO vo = application.create(entity);
        return InvokeResult.ok(vo);
    }

    /* ************************************************************************************ *
       ***************************************  删  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult deleteById(@RequestParam("id") @NotNull Integer id) {
        application.deleteById(id);
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult deleteByIds(@RequestBody @Valid IdsCommand command) {
        application.deleteByIds(command.getIdList());
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult deleteByIpAddr(@RequestParam("ipAddr") @NotBlank String ipAddr) {
        application.deleteByIpAddr(ipAddr);
        return InvokeResult.ok();
    }

    /* ************************************************************************************ *
       ***************************************  改  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult update(@RequestBody @Valid ClientUpdateCommand command) {
        Client client = ClientAssembler.toClient(command);
        client.setOperatorId(getLoginId());
        client.setOperatorName("测试管理员");
        application.update(client);
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult updateServer(@RequestParam("id") @NotNull Integer id,
                                     @RequestParam("serverId") @NotNull Integer serverId) {
        Client client = new Client(id);
        client.setServerId(serverId);
        client.setOperatorId(getLoginId());
        client.setOperatorName("测试管理员");
        Date currentDate = new Date();
        client.setUpdateDate(currentDate);
        application.update(client);
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult updateWorkState(@RequestParam("id") @NotNull Integer id,
                                        @RequestParam("isWork") @NotNull Boolean isWork) {
        Client client = new Client(id);
        client.setIsWork(isWork);
        client.setOperatorId(getLoginId());
        client.setOperatorName("测试管理员");
        Date currentDate = new Date();
        client.setUpdateDate(currentDate);
        application.update(client);
        return InvokeResult.ok();
    }

    /* ************************************************************************************ *
       ***************************************  查  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult<ClientVO> getDetailById(@RequestParam("id") @NotNull int id) {
        ClientVO vo = application.retriveDetailById(id);
        return buildInvokeResult(vo);
    }

    @Override
    public InvokeResult<PageResult<ClientVO>> getPage(PageResult page, @Valid ClientPageCommand command) {
        Client c = ClientAssembler.toClient(command);
        page = application.retrivePageByQuery(page, c);
        return buildPageInvokeResult(page);
    }
}